# -*- coding: utf-8 -*-

from odoo import models, fields, api


class CpqCostCalculatorWizard(models.TransientModel):
    _name = 'cpq.cost.calculator.wizard'
    _description = '成本计算器向导'
    
    # ============ 输入参数 ============
    quotation_id = fields.Many2one(
        'cpq.quotation',
        string='报价单'
    )
    
    product_id = fields.Many2one(
        'product.product',
        string='产品',
        required=True
    )
    
    quantity = fields.Float(
        string='产量',
        required=True,
        default=100000.0
    )
    
    # ============ 物料成本 ============
    material_cost_per_unit = fields.Monetary(
        string='单件物料成本',
        currency_field='currency_id'
    )
    
    waste_rate = fields.Float(
        string='损耗率(%)',
        default=5.0
    )
    
    # ============ 工装摊销 ============
    tooling_total_cost = fields.Monetary(
        string='工装总成本',
        currency_field='currency_id'
    )
    
    # ============ 加工成本 ============
    manufacturing_cost_per_unit = fields.Monetary(
        string='单件加工成本',
        currency_field='currency_id'
    )
    
    # ============ 人工成本 ============
    labor_cost_per_unit = fields.Monetary(
        string='单件人工成本',
        currency_field='currency_id'
    )
    
    # ============ 其他成本 ============
    overhead_rate = fields.Float(
        string='管理费用率(%)',
        default=15.0
    )
    
    currency_id = fields.Many2one(
        'res.currency',
        default=lambda self: self.env.company.currency_id
    )
    
    # ============ 计算结果 ============
    total_cost_per_unit = fields.Monetary(
        string='单件总成本',
        compute='_compute_total_cost',
        currency_field='currency_id'
    )
    
    @api.depends('material_cost_per_unit', 'waste_rate', 'tooling_total_cost',
                 'quantity', 'manufacturing_cost_per_unit', 'labor_cost_per_unit',
                 'overhead_rate')
    def _compute_total_cost(self):
        """计算总成本"""
        for wizard in self:
            # 物料成本(含损耗)
            material_cost = wizard.material_cost_per_unit * (1 + wizard.waste_rate / 100)
            
            # 工装摊销
            if wizard.quantity > 0:
                tooling_amort = wizard.tooling_total_cost / wizard.quantity
            else:
                tooling_amort = 0
            
            # 直接成本
            direct_cost = material_cost + tooling_amort + \
                         wizard.manufacturing_cost_per_unit + \
                         wizard.labor_cost_per_unit
            
            # 间接费用
            overhead = (wizard.manufacturing_cost_per_unit + wizard.labor_cost_per_unit) * \
                      (wizard.overhead_rate / 100)
            
            wizard.total_cost_per_unit = direct_cost + overhead
    
    def action_apply(self):
        """应用计算结果"""
        self.ensure_one()
        
        if self.quotation_id:
            # 更新报价单的成本
            if not self.quotation_id.cost_model_id:
                cost_model = self.env['cpq.cost.model'].create({
                    'quotation_id': self.quotation_id.id,
                    'product_id': self.product_id.id,
                    'estimated_quantity': self.quantity
                })
                self.quotation_id.cost_model_id = cost_model
            
            # 创建成本明细
            cost_model = self.quotation_id.cost_model_id
            cost_model.cost_line_ids.unlink()
            
            # 物料成本
            self.env['cpq.cost.line'].create({
                'cost_model_id': cost_model.id,
                'cost_type': 'material',
                'name': '原材料',
                'unit_cost': self.material_cost_per_unit,
                'quantity': 1.0,
                'waste_rate': self.waste_rate
            })
            
            # 工装摊销
            if self.tooling_total_cost > 0:
                self.env['cpq.cost.line'].create({
                    'cost_model_id': cost_model.id,
                    'cost_type': 'tooling',
                    'name': '模具摊销',
                    'is_amortized': True,
                    'total_tooling_cost': self.tooling_total_cost,
                    'amortization_quantity': self.quantity,
                    'unit_cost': 0,
                    'quantity': 1.0
                })
            
            # 加工成本
            if self.manufacturing_cost_per_unit > 0:
                self.env['cpq.cost.line'].create({
                    'cost_model_id': cost_model.id,
                    'cost_type': 'process',
                    'name': '加工成本',
                    'unit_cost': self.manufacturing_cost_per_unit,
                    'quantity': 1.0
                })
            
            # 人工成本
            if self.labor_cost_per_unit > 0:
                self.env['cpq.cost.line'].create({
                    'cost_model_id': cost_model.id,
                    'cost_type': 'labor',
                    'name': '人工成本',
                    'unit_cost': self.labor_cost_per_unit,
                    'quantity': 1.0
                })
            
            return {
                'type': 'ir.actions.act_window',
                'res_model': 'cpq.quotation',
                'res_id': self.quotation_id.id,
                'view_mode': 'form',
                'target': 'current'
            }

